package examples.session.stateful;

import javax.ejb.Remote;
import javax.ejb.Remove;
import javax.ejb.Stateful;

/**
 * Session Bean implementation class CartBean
 */
@Stateful
@Remote(Cart.class)
public class CartBean implements Cart {

    private int numItems;

    public void addItem() {
      numItems++;
    }

    public int getItems() {
      return numItems;
    }

    @Remove(retainIfException=false)
    public void remove1() throws Exception {
      doRemove();
    }

    @Remove(retainIfException=true)
    public void remove2() throws Exception {
      doRemove();
    }

    private void doRemove() throws Exception {
      if (numItems > 1 && numItems < 4) {
        throw new Exception("blah");
      }
      System.out.println("Removing cart with: "+
        numItems+" items.");
    }

}
